<?php
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
App::uses('MeshtilesApi', 'Lib/Meshtiles');

class MeshtilesAuthenticate extends BaseAuthenticate {
	
	public function authenticate(CakeRequest $request, CakeResponse $response) {
		if (!isset($request->query['access_token']) || !isset($request->query['refresh_token'])) {
			return false;
		}
		
		$userProfile = MeshtilesApi::instance()->callAPI('getUserProfile', array(
			'access_token' => $request->query['access_token']
		));

		if (!$userProfile) {
			return false;
		}
		
		return array(
			'provider' => 'Meshtiles',
			'uid' => $userProfile['user_id'],
			'info' => array(
				'user_name' => $userProfile['user_name'],
				'name' => $userProfile['first_name'] . $userProfile['last_name'],
				//'image' => ??
			),
			'credentials' => array(
				'access_token' => $request->query['access_token'],
				'refresh_token' => $request->query['refresh_token']
				//'expires' => ??
			),
				
		);
	}
}